package Linklist;

/**
 * @Classname JZ24反转链表
 * @Description TODO
 * @Date 2023/2/9 22:37
 * @Created by xjl
 */
public class JZ24反转链表 {

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
    // 使用的是第一种翻转的方式来实现
    public ListNode reverseList(ListNode head) {
        if (head==null){
            return head;
        }
        ListNode pre=null;
        ListNode curr=head;
        while (curr!=null){
            ListNode next=curr.next;
            curr.next=pre;
            pre=curr;
            curr=next;
        }
        return pre;
    }

    public ListNode reverseList2(ListNode head) {
        if (head==null){
            return head;
        }
        ListNode dumpy = new ListNode(-1);
        dumpy.next = head;

        ListNode pre = dumpy;
        ListNode curr = head;

        while (curr.next != null) {
            ListNode future=curr.next;
            curr.next=future.next;
            future.next=dumpy.next;
            pre.next=future;
        }
        return dumpy.next;
    }


    public ListNode reverseList3(ListNode head) {
        if (head==null){
            return head;
        }
        ListNode dumpy=new ListNode(-1);
        dumpy.next=head;

        ListNode pre=dumpy;
        ListNode curr=head;

        while (curr!=null){
            ListNode next=curr.next;
            curr.next=next.next;
            next.next=dumpy.next;
            pre.next=next;
        }
        return dumpy.next;
    }


}
